﻿using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
namespace ChuanYe.CountyFireManagement.BLL
{
    public class LocaInfoBLL
    {
        #region 构造单例
        private LocaInfoBLL() { }
        private static LocaInfoBLL _instance;
        public static LocaInfoBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new LocaInfoBLL());
            }
        }
        #endregion

        public ResultDto<dynamic, dynamic> GetLocaInfoByPage(string addressCounty, RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var sql = " select a.*,b.LocaTypeName,b.LocaImgPath,c.LinchpinName as AddressCountyName,d.LinchpinName as AddressTownName,e.LinchpinName as AddressVillageName  ";
                sql += ",(DATE_ADD(f.IgnoreTime,INTERVAL f.Value MINUTE)) as AlarmNextTime ";
                sql += " from locainfo as a ";
                sql += " left join locatype as b on a.LocaTypeID = b.LocaTypeID ";
                sql += " left join linchpin as c on a.AddressCounty=c.LinchpinID ";
                sql += " left join linchpin as d on a.AddressTown=d.LinchpinID ";
                sql += " left join linchpin as e on a.AddressVillage=e.LinchpinID ";
                sql += " left join alarmignoretime as f on a.LocaID =f.LocaID ";

                sql += " where 1=1 ";
                if (!string.IsNullOrEmpty(addressCounty))
                {
                    sql += " and a.addressCounty = '{0}'";
                    sql = string.Format(sql, addressCounty);
                }
                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    sql += " and a.LocaName like '%{0}%'";
                    sql = string.Format(sql, page.Keyword);
                }
                sql += page.OrderByPattern == 0 ? "a.AddTime".OrderByDescending() : "a.AddTime".OrderBy();
                sql += page.Limit();
                var list = ef.Database.SqlQuery<ChuanYe.CountyFireManagement.Model.LocaInfoModel>(sql);
                foreach (var item in list)
                {
                    var locaVideoList = ef.Locavideo.Where(p => p.LocaId == item.LocaId).ToList();
                    var nUnits = locaVideoList.Select(s => s.NUnit).ToList();
                    item.AlarmCount = nUnits.Count == 0 ? 0 : ef.Videoalarm.Where(p => nUnits.Contains(p.Nunit)).ToList().Count();
                    item.Videos = locaVideoList;
                }

                var countSql = sql.CountSql();
                var count = ef.Database.SqlQuery(countSql).Rows[0][0];
                result.FirstParam = list;
                result.SecondParam = count;
                return result;
            }
        }

        public ResultDto<dynamic> GetLocaInfoByTree(string addressCounty)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var sql = " select a.*,b.LocaTypeName,b.LocaImgPath,c.LinchpinName as AddressCountyName,d.LinchpinName as AddressTownName,e.LinchpinName as AddressVillageName  ";
                sql += ",(DATE_ADD(f.IgnoreTime,INTERVAL f.Value MINUTE)) as AlarmNextTime ";
                sql += " from locainfo as a ";
                sql += " left join locatype as b on a.LocaTypeID = b.LocaTypeID ";
                sql += " left join linchpin as c on a.AddressCounty=c.LinchpinID ";
                sql += " left join linchpin as d on a.AddressTown=d.LinchpinID ";
                sql += " left join linchpin as e on a.AddressVillage=e.LinchpinID";
                sql += " left join alarmignoretime as f on a.LocaID =f.LocaID ";

                sql += " where 1=1 ";
                if (!string.IsNullOrEmpty(addressCounty))
                {
                    sql += " and a.addressCounty = '{0}'";
                    sql = string.Format(sql, addressCounty);
                }

                var list = ef.Database.SqlQuery<ChuanYe.CountyFireManagement.Model.LocaInfoModel>(sql);
                foreach (var item in list)
                {
                    var locaVideoList = ef.Locavideo.Where(p => p.LocaId == item.LocaId).ToList();
                    var nUnits = locaVideoList.Select(s => s.NUnit).ToList();
                    item.AlarmCount = nUnits.Count == 0 ? 0 : ef.Videoalarm.Where(p => nUnits.Contains(p.Nunit)).ToList().Count();
                    item.Videos = locaVideoList;
                }

                List<LocaInfoTreeModel> models = new List<LocaInfoTreeModel>();
                var addressCountyIDs = list.GroupBy(p => new { p.AddressCounty, p.AddressCountyName })
                                           .Select(s => new { s.Key.AddressCounty, s.Key.AddressCountyName }).ToList();
                foreach (var county in addressCountyIDs)
                {
                    LocaInfoTreeModel model = new LocaInfoTreeModel();
                    model.NodeId = county.AddressCounty;
                    model.NodeName = county.AddressCountyName;
                    model.Child = new List<LocaInfoChildTreeModel>();
                    var addressTowns=list.Where(p => p.AddressCounty == county.AddressCounty)
                                         .GroupBy(p => new {p.AddressTown,p.AddressTownName })
                                         .Select(s=>new { s.Key.AddressTown,s.Key.AddressTownName}).ToList();
                    foreach (var town in addressTowns)
                    {
                        model.Child.Add(new LocaInfoChildTreeModel() { 
                            NodeId= town.AddressTown,
                            NodeName=town.AddressTownName,
                            List=list.Where(p=>p.AddressCounty==county.AddressCounty && p.AddressTown== town.AddressTown).ToList(),
                        });
                    }
                    models.Add(model);
                 }
                result.FirstParam = models;
                return result;
            }

        }
        public ResultDto<int> AddLocaInfo() 
        {
            return null;
        }

        public ResultDto<int> UpdateLocaInfo() 
        {
            return null;
        }
    }

   
}  
